﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace RescueRobotController.Panels
{
    public partial class ChassisPanel : DockContent
    {
        private Timer autoSpeedTimer;

        // 下面的内容是每个面板必须要有的
        // *********** BEGIN ************* //
        // “发送AT指令”事件
        public delegate void ExistATCmdToSendHandler(ATCmd cmd);
        public event ExistATCmdToSendHandler SendATCmd;

        // 处理AT指令
        public void HandleATCmd(ATCmd cmd)
        {

        }
        // *********** END ************* //

        public ChassisPanel()
        {
            InitializeComponent();
        }

        public void AutoSpeedStart()
        {
            // 检查是否开启自动油门
            if (check_auto_speed.Checked && Common.isSerialOpened)
            {
                autoSpeedTimer = new Timer
                {
                    Interval = 100
                };
                autoSpeedTimer.Tick += AutoSpeedTimer_Tick;
                autoSpeedTimer.Start();
            }
        }

        private void AutoSpeedEnd()
        {
            if (check_auto_speed.Checked)
            {
                autoSpeedTimer?.Stop();
                numeric_speed.Value = 0;
            }
        }

        private void AutoSpeedTimer_Tick(object sender, EventArgs e)
        {
            if (numeric_speed.Value < 100)
                numeric_speed.Value += 1;
        }

        private void btn_forward_Click(object sender, EventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_FORWARD"
            });
        }

        private void btn_backward_Click(object sender, EventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_BACKWARD"
            });
        }

        private void btn_turnleft_Click(object sender, EventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_TURNLEFT"
            });
        }

        private void btn_turnright_Click(object sender, EventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_TURNRIGHT"
            });
        }

        private void numeric_speed_ValueChanged(object sender, EventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.SET,
                target = "CHASSIS_SPEED",
                value = numeric_speed.Value.ToString()
            });
        }

        private void btn_forward_MouseDown(object sender, MouseEventArgs e)
        {
            numeric_speed_ValueChanged(sender, null);
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_FORWARD"
            });
            AutoSpeedStart();
        }

        private void btn_forward_MouseUp(object sender, MouseEventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_STOP"
            });
            AutoSpeedEnd();
        }

        private void btn_backward_MouseDown(object sender, MouseEventArgs e)
        {
            numeric_speed_ValueChanged(sender, null);
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_BACKWARD"
            });
            AutoSpeedStart();
        }

        private void btn_backward_MouseUp(object sender, MouseEventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_STOP"
            });
            AutoSpeedEnd();
        }

        private void btn_turnleft_MouseDown(object sender, MouseEventArgs e)
        {
            numeric_speed_ValueChanged(sender, null);
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_TURNLEFT"
            });
        }

        private void btn_turnleft_MouseUp(object sender, MouseEventArgs e)
        {
            numeric_speed_ValueChanged(sender, null);
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_STOP"
            });
        }

        private void btn_turnright_MouseDown(object sender, MouseEventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_TURNRIGHT"
            });
        }

        private void btn_turnright_MouseUp(object sender, MouseEventArgs e)
        {
            SendATCmd(new ATCmd()
            {
                type = ATCmdType.EXEC,
                target = "CHASSIS_STOP"
            });
        }

        private void btn_forward_Click_1(object sender, EventArgs e)
        {

        }

        private void ChassisPanel_Load(object sender, EventArgs e)
        {

        }
    }
}
